<?php

/**
 * This is the model class for table "member".
 *
 * The followings are the available columns in table 'member':
 * @property string $MemberID
 * @property string $MemberName
 * @property string $LoginName
 * @property string $Password
 * @property string $Email
 * @property string $Phone
 * @property string $Mobile
 * @property string $Address
 * @property integer $IsActive
 * @property string $Birthday
 * @property string $CreateDate
 * @property integer $MemberGroupID
 */
class Member extends CActiveRecord
{
	
   public $repeatPassword;
   public $oldPassword;
   /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'member';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
         array('MemberName,LoginName,Password,repeatPassword,Email', 'required',
               'message' => "{attribute} không được bỏ trống",'on' => 'insert,update'),
         array('LoginName','match','pattern' => '/^[A-Za-z0-9_]+$/u','message' => 'Tên đăng nhập chỉ được chứa các ký tự từ a-z và số'),
         array('repeatPassword','compare','compareAttribute' => 'Password','message' => 'Xác nhận mật khẩu không chính xác','on' => 'insert'),
			array('Email','email','message' => 'Định đang email không chính xác'),
         array('LoginName','validateLoginName','on' => 'insert'),
         array('IsActive,IsAdmin, MemberGroupID, CreateDate', 'numerical', 'integerOnly'=>true),
			array('MemberName, LoginName, Email', 'length', 'max'=>50),
			array('Password', 'length', 'max'=>32),
         array('Password','validatePassword','on' => 'insert'),
			array('Phone, Mobile', 'length', 'max'=>20),
			array('Address', 'length', 'max'=>150),
			array('Birthday', 'safe'),
         array('MemberID, MemberName, LoginName, Password, repeatPassword, Email, Phone, Mobile, Address, IsActive, Birthday, CreateDate, MemberGroupID', 'safe' , 'on' => 'insert'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('MemberID, MemberName, LoginName, Password, Email, Phone, Mobile, Address, IsActive, Birthday, CreateDate, MemberGroupID', 'safe', 'on'=>'search'),
		);
	}
   
   public function validatePassword($attribute,$params){
      if(strlen($this->Password) < 8){
         $this->addError($attribute,'Mật khẩu phải dài 8 ký tự trở lên');
      }
   }
   
   public function validateLoginName($attribute,$params){
      
      $row = $this->findByAttributes(array("LoginName" => $this->LoginName));
      if(!is_null($row)){
         $this->addError($attribute,'Tên đăng nhập này đã tồn tại');
      }
   }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'MemberID' => 'ID',
			'MemberName' => 'Tên thành viên',
			'LoginName' => 'Tên đăng nhập',
			'Password' => 'Mật khẩu',
         'repeatPassword' => 'Xác nhận mật khẩu',
         'oldPassword' => 'Mật khẩu cũ',
			'Email' => 'Email',
			'Phone' => 'SĐT cố định',
			'Mobile' => 'Di động',
			'Address' => 'Địa chỉ',
			'IsActive' => 'Kích hoạt',
			'Birthday' => 'Sinh nhật',
			'CreateDate' => 'Ngày tạo',
			'MemberGroupID' => 'Nhóm',
         'MembergroupName' => 'Tên nhóm'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('MemberID',$this->MemberID,true);
		$criteria->compare('MemberName',$this->MemberName,true);
		$criteria->compare('LoginName',$this->LoginName,true);
		$criteria->compare('Password',$this->Password,true);
		$criteria->compare('Email',$this->Email,true);
		$criteria->compare('Phone',$this->Phone,true);
		$criteria->compare('Mobile',$this->Mobile,true);
		$criteria->compare('Address',$this->Address,true);
		$criteria->compare('IsActive',$this->IsActive);
		$criteria->compare('Birthday',$this->Birthday,true);
		$criteria->compare('CreateDate',$this->CreateDate,true);
		$criteria->compare('MemberGroupID',$this->MemberGroupID);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Member the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
   
   /**
    * Check user login. If user logged return id
    * 
   */
   public static function checkLogged(){
      $id = Yii::app()->user->id;
      if(is_null($id))
         return 0;
      else
         return $id;
         
   }
}
